<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery1">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="门店" :required="true">
              <a-cascader
                :options="departModel.departTreeOptions"
                :showSearch="true"
                expand-trigger="hover"
                placeholder
                :fieldNames="departModel.fieldNames"
                style="width: 200px"
                v-model="queryParam.selecteddeparts"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品编码">
              <a-input placeholder="" v-model="queryParam.itemCode" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="SKU编码">
              <a-input placeholder="" v-model="queryParam.itemSku" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
          <a-form-item label="业务类型">
              <a-select v-model="queryParam.bizType" allowClear>
                <a-select-option
                  v-for="(item, index) in bizType"
                  :key="index"
                  :value="item.value"
                >{{ item.text }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery1" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset1" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table 
        ref="table"
        size="middle"
        bordered
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >

      </a-table>
    </div>
    <!-- table区域-end -->
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { queryDepartTreeList1 } from '@/api/api'

export default {
  name: "PosShopGoodsStockChangeList",
  mixins: [JeecgListMixin],
  components: {
    
  },
  data() {
    return {
      description: '门店商品库存变动记录管理页面',
      disableMixinCreated: true,
      departModel: {
        departTreeOptions: [],
        fieldNames: { label: 'title', value: 'value', children: 'children' }
      },
      // 表头
      columns: [
        {
          title: '机构',
          align: "center",
          dataIndex: 'orgName'
        },
        {
          title: '门店',
          align: "center",
          dataIndex: 'shopName'
        },
        {
          title: '商品编码',
          align: "center",
          dataIndex: 'itemCode'
        },
        {
          title: 'SKU编码',
          align: "center",
          dataIndex: 'itemSku'
        },
        {
          title: '业务类型',
          align: "center",
          dataIndex: 'bizType'
        },
        {
          title: '变更数量',
          align: "center",
          dataIndex: 'ioQty'
        },
        {
          title: '变更后数量',
          align: "center",
          dataIndex: 'afterQty'
        },
        {
          title: '操作人姓名',
          align: "center",
          dataIndex: 'operUserName'
        },
        {
          title: '库存变动时间',
          align: "center",
          dataIndex: 'changeTime'
        },
        

      ],
      //入库,订单,退货,调入,调出,返货,盘盈,盘亏
      bizType: [
        { text: '入库', value: '入库' },
        { text: '订单', value: '订单' },
        { text: '退货', value: '退货' },
        { text: '调入', value: '调入' },
        { text: '调出', value: '调出' },
        { text: '返货', value: '返货' },
        { text: '盘盈', value: '盘盈' },
        { text: '盘亏', value: '盘亏' },
      ],
      url: {
        list: "/stock/posShopGoodsStockChange/pageList",
      },
    }
  },
  computed: {
  },
  created(){
    this.beforeCreate()
  },
  methods: {
    beforeCreate(){
      this.loadDepartTree()
    },
    loadDepartTree() {
      queryDepartTreeList1().then(res => {
        if (res.success) {
          let departList = res.result
          console.log('门店信息' + departList)
          if (departList.length == 1 && departList[0].orgType == 1) {
            departList = departList[0].children
          }
          this.departModel.departTreeOptions = departList

          this.departModel.departTreeOptions.forEach(x => {
            if (!x['children']) {
              x['children'] = []
            }
            let allShop = { title: '全部门店', value: '0' }
            x['children'].unshift(allShop)
          })

          this.$set(this.queryParam, 'selecteddeparts', [departList[0].id, '0'])
        }
        this.searchQuery1()
      })
    },
    searchReset1(){
    
      let selecteddeparts = this.queryParam.selecteddeparts
      this.queryParam = {selecteddeparts: selecteddeparts}
      this.searchQuery1()

    },
    searchQuery1() {
      console.log('查询参数是：', this.queryParam.selecteddeparts)

      let selecteddeparts = this.queryParam.selecteddeparts
      if (selecteddeparts && selecteddeparts.length == 2) {
        this.queryParam.orgIds = selecteddeparts[0]
        this.queryParam.shopIds = selecteddeparts[1] == 0 ? null : selecteddeparts[1]

      } else {
        this.queryParam.orgIds = null
        this.queryParam.shopIds = null
        
      }


      let orgIds = this.queryParam.orgIds
      if (!orgIds) {
        this.$message.warning('请选择机构再查询')
        return
      }

      // 在此调用查询数量
      this.loadData(1)
    },

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>